package com.white.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.Scanner;

public class ClientB {
    public static void main(String[] args) throws Exception{
        Scanner in = new Scanner(System.in);

        while (true) {
            System.out.println("客户端：");
            String input = in.nextLine().trim();
            if ("q".equals(input)) {
                System.out.println("退出");
                break;
            }
            AsynchronousSocketChannel socketChannel = AsynchronousSocketChannel.open();
            socketChannel.connect(new InetSocketAddress("localhost", 8080), null,
                    new CompletionHandler<Void, Object>() {
                        @Override
                        public void completed(Void result, Object attachment) {
                            try {
                                socketChannel.write(ByteBuffer.wrap(input.getBytes()));
                                socketChannel.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void failed(Throwable exc, Object attachment) {

                        }
                    });
            Thread.sleep(1000);
        }
    }

}
